package source
{
	import com.justinbuser.net.DynamicURLLoader;
	import com.justinbuser.singleton.Core;
	
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import source.MainApplication;
	
	import spark.components.Callout;
	import spark.components.TabbedViewNavigator;
	import spark.components.View;
	import spark.components.ViewMenuItem;
	import spark.components.ViewNavigatorApplication;
	import spark.components.supportClasses.ViewNavigatorApplicationBase;
	
	import views.AwardsView;
	import views.ScoresView;
	
	[Bindable] public class FullReportView extends View
	{
		public var reportNavigator:TabbedViewNavigator;
		public var fullBattleReport:FullBattleReport;
		public var saveMenuItem:ViewMenuItem;
		public var savedOk:Callout;
		private var core:Core = Core.core;
		
		public function FullReportView()
		{
			super();
			//http://battlelog.battlefield.com/bf3/gate/login/?redirect=|bf3|battlereport|loadLatestPlayerPreviews|10|&email=backdoorcharley@boogiemanclan.com&password=bertani
			
		}
		
		public function saveReport():void{
			if(!core.savedReports)
			{
				core.savedReports = new Array();		
			}
			core.savedReports.push(BattleReport(data.valueOf()));
			savedOk.open(this);
		}
		
		public function init():void{
			viewMenuItems = MainApplication.mainViewMenuItems.concat();
			viewMenuItems.unshift(saveMenuItem);
			reportNavigator.hideTabBar(false);
			/*if(data is BattleReport)
			{
				fullBattleReport = BattleReport(data).fullReport;
			}
			else if(data.fullReport)
			{
				
			}*/
			data = BattleReport(data);
			if(BattleReport(data).fullReport)
			{
				fullBattleReport = BattleReport(data).fullReport;
			}
			else
			{
				BattleReport(data).getFullReport(parseReport);
				return;
			}
			
			loadTeams();
			
			function parseReport(o:FullBattleReport):void{
				fullBattleReport = BattleReport(data).fullReport;
				loadTeams();
			}
			
			function loadTeams():void{
				reportNavigator.visible = false;
				for(var i:int = 0; i < fullBattleReport.teams.length; i++)
				{
					var team:Object = fullBattleReport.teams[i];
					var scoreView:ScoresView = new ScoresView();
					scoreView.label = team.winners ? "Winner: "+team.label : "Loser: "+team.label;
					scoreView.personas = fullBattleReport.teams[i].personas;
					reportNavigator.addItem(scoreView);
				}
				var awardsView:AwardsView = new AwardsView();
				awardsView.label = "Awards";
				awardsView.fullBattleReport = fullBattleReport;
				reportNavigator.addItemAt(awardsView,0);
				reportNavigator.selectedIndex = 0;
				reportNavigator.visible = true;
				reportNavigator.showTabBar(true);
			}
		}
		
	}
}